import table from '@/components/dt/table/render.js'
import nameOpt from '@/biz/option/name.js'
import dateOpt from '@/biz/option/date.js'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'
import dt from '@dt/dt'
import planOpt from '@/biz/option/plan.js'

let _this = null;

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [{
      title: '签约单编号',
      field: 'contractCode',
      minWidth: 150,
    },
    {
      title: '签约状态',
      field: 'status',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          return dt.dictInfo().__value('status', params.row.status)
        }
      }
    },
    {
      minWidth: 130,
      field: 'counselorName',
      header: {
        filter: nameOpt.createCounselorPeople({
          key: 'counselorId',
          title: '归属顾问',
          to: (info) => {
            return {
              counselorId: info.userId
            }
          }
        })
      }
    },
    {
      field: 'customerName',
      minWidth: 150,
      title: '客户姓名',
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: params.row.customerName,
            event: 'onLinkUser',
            data: params.row
          })
        }
      },
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {},
        },
      }
    },
    {
      title: '客户手机号',
      field: 'customerPhone',
      minWidth: 150,
      header: {
        filter: {
          title: '客户手机号',
          key: 'customerPhone',
          placeholder: '请输入至少4位客户手机号',
          input: {},
          check: (val) => {
            if (val.customerPhone.length < 4) {
              _this.$Message.warning('请输入至少4位客户手机号。')
            }
            return !(val.customerPhone.length < 4)
          }
        },
      }
    },
    {
      title: '签约房源',
      field: 'roomName',
      minWidth: 180,
      header: {
        filter: houseOpt.createRoom({
          key: 'roomId',
          title: '签约房源'
        })
      }
    },
    {
      title: '面积(㎡)',
      field: 'area',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: '签约单价(元)',
      field: 'unitPrice',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: '签约总价(元)',
      field: 'totalPrice',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: '签约日期',
      field: 'contractDate',
      minWidth: 150,
      header: {
        filter: dateOpt.range({
          title: '签约日期',
          startKey: 'contractStartDate',
          endKey: 'contractEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (_this.$tool.dateDiff(val.contractStartDate, val
                .contractEndDate).Y >= 2) {
              // 日期最大跨度两年
              _this.$Message.warning('签约日期跨度至多两年。')
            }
            return !(_this.$tool.dateDiff(val.contractStartDate, val
              .contractEndDate).Y >= 2)
          }
        }),
      }
    },
    {
      title: '所属产品',
      field: 'productName',
      minWidth: 180,
      visible: _this.showTab != 'INVALID'
    },
    {
      title: '渠道名称',
      field: 'channelName',
      minWidth: 180,
      header: {
        filter: houseOpt.createChannel({
          key: 'channelId',
          title: '渠道名称'
        })
      }
    },
    {
      title: '合作类型',
      field: 'channelType',
      minWidth: 150
    },
    {
      title: '渠道费用(元)',
      field: 'channelCostValue',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'channelFitName',
      title: '打法名称',
      width: 130,
      header: {
        filter: planOpt.create({
          key: 'planId',
          title: '打法名称',
          data:{
            to: (info) => {
              let channelIdList = []
              info.forEach(ele => {
                let channelId = ele.value.split('/')[1]
                channelIdList.push(channelId)
              });
              return {
                channelIdList: channelIdList.join(',').split(',')
              }
            }
          }
        })
      }
    },
    {
      title: '签约人',
      field: 'contractorName',
      minWidth: 150,
      visible: _this.showTab != 'INVALID'
    },
    {
      title: '累计到账房款（元）',
      field: 'accumulateBill',
      minWidth: 180,
      cell: {
        text: {
          type: 'number'
        }
      },
      visible: _this.showTab != 'INVALID'
    },
    {
      title: '全款到账日期',
      field: 'accountDate',
      minWidth: 150,
      header: {
        filter: dateOpt.range({
          title: '全款到账日期',
          startKey: 'accountDateStart',
          endKey: 'accountDateEnd',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (_this.$tool.dateDiff(val.accountDateStart, val
                .accountDateEnd).Y >= 2) {
              // 日期最大跨度两年
              _this.$Message.warning('全款到账日期跨度至多两年。')
            }
            return !(_this.$tool.dateDiff(val.accountDateStart, val
              .accountDateEnd).Y >= 2)
          }
        }),
      },
      visible: _this.showTab != 'INVALID'
    },
    {
      title: '交房单编号',
      field: 'handoverCode',
      minWidth: 180,
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '合同编号',
      field: 'bargainCode',
      minWidth: 180,
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '预告登记日期',
      field: 'advanceRegisterDate',
      minWidth: 150,
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '抵押收件日期',
      field: 'pledgeConsigneeDate',
      minWidth: 150,
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '交房日期',
      field: 'handRoomDate',
      minWidth: 150,
      header: {
        filter: dateOpt.range({
          title: '交房日期',
          startKey: 'handoverDateStart',
          endKey: 'handoverDateEnd',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (_this.$tool.dateDiff(val.handoverDateStart, val
                .handoverDateEnd).Y >= 2) {
              // 日期最大跨度两年
              _this.$Message.warning('交房日期跨度至多两年。')
            }
            return !(_this.$tool.dateDiff(val.handoverDateStart, val
              .handoverDateEnd).Y >= 2)
          }
        }),
      },
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '交房人',
      field: 'handoverName',
      minWidth: 150,
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '交房奖占比(%)',
      field: 'divideInto',
      minWidth: 180,
      visible: _this.showTab == 'HANDOVER',
    },
    {
      title: '作废交房单号',
      field: 'handoverCode',
      minWidth: 180,
      visible: _this.showTab == 'INVALID',
    },
    {
      title: '作废日期',
      field: 'invalidDate',
      minWidth: 150,
      header: {
        filter: dateOpt.range({
          title: '作废日期',
          startKey: 'invalidDateStart',
          endKey: 'invalidDateEnd',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (_this.$tool.dateDiff(val.invalidDateStart, val
                .invalidDateEnd).Y >= 2) {
              // 日期最大跨度两年
              _this.$Message.warning('作废日期跨度至多两年。')
            }
            return !(_this.$tool.dateDiff(val.invalidDateStart, val
              .invalidDateEnd).Y >= 2)
          }
        }),
      },
      visible: _this.showTab == 'INVALID',
    },
    {
      title: '作废原因',
      field: 'invalidReason',
      minWidth: 180,
      visible: _this.showTab == 'INVALID',
    },
    {
      title: '操作人',
      field: 'operatorName',
      minWidth: 180,
      visible: _this.showTab == 'INVALID',
    },
    {
      title: '操作',
      fixed: 'right',
      width: 140,
      align: 'center',
      slots: {
        default: (params, h) => {
          let btns = []
          btns.push({
            title: '查看',
            event: 'viewFun',
            data: params.row
          })
          btns.push({
            title: _this.showTab == 'ABLE' && _this.unlocking(
                '0104050400') ?
              '交房' : '',
            event: 'deliverAHouseFun',
            data: params.row
          })
          btns.push({
            title: _this.showTab == 'HANDOVER' && _this.unlocking(
                '0104050400') ?
              '修改' : '',
            event: 'editDeliverAHouseFun',
            data: params.row
          })
          return table.cell.btns(_this, h, btns)
        }
      }
    }, ]
  }

}


export default Column